package com.sbpm.crmbpm.shared.data.organisation;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table( name = "USER_ROLE_REL" )
@IdClass( UserRoleRel.UserRole.class )
public class UserRoleRel implements Serializable
{

    @Id
    @Column( name = "USER_ID" )
    private String userId;

    @Id
    @Column( name = "ROLE_ID" )
    private String roleId;

    @Column( name = "IS_ACTIVE" )
    private boolean isActive;

    @ManyToOne( fetch = FetchType.LAZY )
    @JoinColumn( name = "USER_ID", insertable = false, updatable = false )
    private User user;

    @ManyToOne( fetch = FetchType.LAZY )
    @JoinColumn( name = "ROLE_ID", insertable = false, updatable = false )
    private Role role;

    public String getUserId()
    {
        return userId;
    }

    public void setUserId( String userId )
    {
        this.userId = userId;
    }

    public String getRoleId()
    {
        return roleId;
    }

    public void setRoleId( String roleId )
    {
        this.roleId = roleId;
    }

    public boolean isActive()
    {
        return isActive;
    }

    public void setActive( boolean isActive )
    {
        this.isActive = isActive;
    }

    public User getUser()
    {
        return user;
    }

    public void setUser( User user )
    {
        this.user = user;
    }

    public Role getRole()
    {
        return role;
    }

    public void setRole( Role role )
    {
        this.role = role;
    }

    public static class UserRole implements Serializable
    {
        protected String userId;
        protected String roleId;

        public UserRole( String userId, String roleId )
        {
            this.userId = userId;
            this.roleId = roleId;
        }

        public UserRole()
        {
        }

        public String getUserId()
        {
            return userId;
        }

        public void setUserId( String userId )
        {
            this.userId = userId;
        }

        public String getRoleId()
        {
            return roleId;
        }

        public void setRoleId( String roleId )
        {
            this.roleId = roleId;
        }

        @Override
        public boolean equals( Object o )
        {
            if ( this == o )
            {
                return true;
            }
            if ( o == null || getClass() != o.getClass() )
            {
                return false;
            }

            UserRole userRole = ( UserRole ) o;

            if ( roleId != null ? !roleId.equals( userRole.roleId ) : userRole.roleId != null )
            {
                return false;
            }
            if ( userId != null ? !userId.equals( userRole.userId ) : userRole.userId != null )
            {
                return false;
            }

            return true;
        }

        @Override
        public int hashCode()
        {
            int result = userId != null ? userId.hashCode() : 0;
            result = 31 * result + ( roleId != null ? roleId.hashCode() : 0 );
            return result;
        }
    }

    @Override
    public boolean equals( Object o )
    {
        if ( this == o )
        {
            return true;
        }
        if ( o == null || getClass() != o.getClass() )
        {
            return false;
        }

        UserRoleRel that = ( UserRoleRel ) o;

        if ( roleId != null ? !roleId.equals( that.roleId ) : that.roleId != null )
        {
            return false;
        }
        if ( userId != null ? !userId.equals( that.userId ) : that.userId != null )
        {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode()
    {
        int result = userId != null ? userId.hashCode() : 0;
        result = 31 * result + ( roleId != null ? roleId.hashCode() : 0 );
        return result;
    }
}
